package com.adhere.system.service.impl;

import com.adhere.system.mapper.RbacUserRoleMapper;
import com.adhere.system.pojo.dto.UserRoleDto;
import com.adhere.system.service.IRbacPrivilegeService;
import com.adhere.system.service.IRbacUserRoleService;
import com.adhere.system.pojo.RbacPrivilege;
import com.adhere.system.pojo.RbacUserRole;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * <p>
 * 用户角色关系表中的行记录删除执行的是“逻辑真删除”逻辑，一个用户可具备多种角色 服务实现类
 * </p>
 *
 * @author 
 * @since 2021-04-02
 */
@Service
public class RbacUserRoleServiceImpl extends ServiceImpl<RbacUserRoleMapper, RbacUserRole> implements IRbacUserRoleService {

    @Autowired
    private RbacUserRoleMapper userRoleMapper;

    @Autowired
    private IRbacPrivilegeService privilegeService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public List<RbacUserRole> selectByUserCode(String userCode) {
        QueryWrapper<RbacUserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_code", userCode);
        return userRoleMapper.selectList(queryWrapper);
    }

    @Override
    public List<RbacUserRole> selectByRoleCode(String roleCode) {
        QueryWrapper<RbacUserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_code", roleCode);
        return userRoleMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional
    public void updateUserRole(UserRoleDto userRoleDto) {
        List<String> roleCodeList = userRoleDto.getRoleCodeList();
        String userCode = userRoleDto.getUserCode();

        // 将此用户拥有的角色全部删除
        QueryWrapper<RbacUserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_code", userCode);
        userRoleMapper.delete(queryWrapper);

        if (!ObjectUtils.isEmpty(roleCodeList)) {
            // 添加传过来的权限
            roleCodeList.forEach(roleCode -> {
                RbacUserRole rbacUserRole = new RbacUserRole().setRoleCode(roleCode).setUserCode(userCode);
                userRoleMapper.insert(rbacUserRole);
            });

            // 更新用户在redis中的权限信息
            List<RbacPrivilege> privilegeList = privilegeService.getPrivilegeByRoleList(roleCodeList);
            try {
                String privilegeListJson = objectMapper.writeValueAsString(privilegeList);
                redisTemplate.opsForValue().set(userCode, privilegeListJson);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        } else {
            // 清楚redis中此用户的权限
            redisTemplate.delete(userCode);
        }

    }

}
